/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.pic.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BizPicMapper;
import com.smooth.dao.gen.bean.BizPic;
import com.smooth.dao.gen.bean.BizPicExample;
import com.smooth.site.sx.mag.biz.pic.enums.PicTypeEnums;

/**
 * 图片信息Service
 * @author bm
 * @version 2019-02-26
 */
@Service
@Transactional(readOnly = true)
public class BizPicService{
	Logger log = LoggerFactory.getLogger(BizPicService.class);
	@Autowired
	BizPicMapper bizPicMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizPic get(String no) {
		return bizPicMapper.selectByPrimaryKey(no);
	}
	
	public List<BizPic> findList(BizPic bizPic) {
		BizPicExample bizPicExample = new BizPicExample();
		BizPicExample.Criteria criteria = bizPicExample.createCriteria();
		
		return bizPicMapper.selectByExample(bizPicExample);
	}
	
	public Page<BizPic> findPage(Page<BizPic> page, BizPic bizPic) {
		BizPicExample bizPicExample = new BizPicExample();
		BizPicExample.Criteria criteria = bizPicExample.createCriteria();
		
		bizPicExample.setPage(page);
		
		List<BizPic> dlist = bizPicMapper.selectByExample(bizPicExample);
		page.setList(dlist);
		page.setCount(bizPicMapper.countByExample(bizPicExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public BizPic save(BizPic bizPic) {
		if(StringUtils.isNotEmpty(bizPic.getId())&&get(bizPic.getId())!=null) {
			int optNum = bizPicMapper.updateByPrimaryKeySelective(bizPic);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizPic.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizPic.toString()+"】失败");
			}
			return bizPicMapper.selectByPrimaryKey(bizPic.getId());
		}else {
			Date date = new Date();
			bizPic.setCreateDate(date);
			bizPic.setId(idGenerater.genClsNo("bizPic"));
			bizPic.setUpdateDate(date);
			int optNum =  bizPicMapper.insertSelective(bizPic);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizPic.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizPic.toString()+"】失败");
			}
			return bizPic;
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BizPic bizPic) {
		bizPicMapper.deleteByPrimaryKey(bizPic.getId());
	}
	
	public List<BizPic> queryPicByType(PicTypeEnums picTypeEnums) {
		BizPicExample bizPicExample = new BizPicExample();
		bizPicExample.createCriteria().andImgTypeEqualTo(picTypeEnums.value);
		
		bizPicExample.setOrderByClause(" create_date desc ");
		
		List<BizPic> bplist = bizPicMapper.selectByExample(bizPicExample);
		return bplist;
	}
	
}